<?php

ini_set("display_errors", "On");
error_reporting(E_ALL);

// 引入数据库连接文件
require_once '../../sql/connection.php';

$stuNum = $_POST['stuNum'];
$stuName = $_POST['stuName'];
$className = $_POST['className'];
$pwd = password_hash($_POST['pwd'], PASSWORD_DEFAULT);

$querySql = "select id from student_info where stu_ID='$stuNum'";


// 查询语句
// 连接数据库，并查询
$result = mysqli_query($GLOBALS['conn'], $querySql);

//echo $result_config;
if (mysqli_num_rows($result) > 0) {
	echo "
<script>
    alert('该学号已注册！');
    history.go(-1);
</script>";
} else {
	
	$querySql = "select id from class_info where name='$className'";
	$result = mysqli_query($GLOBALS['conn'], $querySql);
	$classRow = mysqli_fetch_array($result);
	
	
	if (mysqli_num_rows($result) > 0)
	{
		$classID = $classRow['id'];
		$now = date('Y-m-d H:i:s', time());
		$insertSql = "INSERT INTO student_info(create_time, update_time, stu_ID, stu_name, stu_pwd, class_ID, is_auth, is_delete) VALUES ('$now','$now','$stuNum','$stuName','$pwd','$classID',0,0);";
		
		if (mysqli_query($GLOBALS['conn'], $insertSql)){
			echo "
            <script>
                alert('注册成功，等待教师验证！');
                window.location.href='/student/#';
            </script>
        ";
		}else{
			echo "
            <script>
                alert('注册失败！');
                history.back();
            </script>
        ";
		}
	}
	else{
		echo "
            <script>
                alert('无该班级！');
                // history.back();
            </script>
        ";
	}
}

